# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.exceptions import ValidationError


class InheritProductCategory(models.Model):
    _inherit = 'product.category'

    product_tax_batch = fields.Many2one(comodel_name='account.tax', company_dependent=True, string='分類銷售稅',
                                        help='全部公司的產品都會設定這個稅額名稱')

    def set_all_category_product(self):

        tax = self.env['account.tax'].sudo().search([('type_tax_use', '=', self.product_tax_batch.type_tax_use),
                                                     ('name', '=', self.product_tax_batch.name)])
        total = []
        for line in tax:
            total.append([4, line.id])
        for line in self.env['product.template'].search([('categ_id', '=', self.id)]):
            line.write({
                'taxes_id': total
            })
        # if company.parent_id.id > 0:
        #     tax = self.env['account.tax'].search([('type_tax_use', '=', 'sale'), ('price_include', '=', True)])
        # elif len(company.child_ids) > 0:
        #     tax = self.env['account.tax']
        #     for line in company.child_ids:
        #         row = self.env['account.tax'].search([('type_tax_use', '=', 'sale'), ('price_include', '=', True),
        #                                               ('company_id', '=', line.id)])
        #         tax += row
        #
        # if tax is None:
        #     raise ValidationError(u'錯誤')



